﻿package 
{
	import Base.Debug.DebugManager;
	import Base.Loader.*;
	import Base.Log.LogManager;
	import Base.Sound.SoundManager;
	
	import Common.*;
	
	import Events.*;
	
	import Module.ModuleManager;
	
	import Stage.GameStageManager;
	
	import flash.display.*;
	import flash.events.*;
	import flash.system.Security;
	import flash.ui.Mouse;
	import flash.ui.MouseCursorData;

	
	
	/**
	 * ...
	 * @author fox
	 */
	[SWF( width = "800" , height = "568" , backgroundColor = "#000000" , frameRate = "30" )]
	
	
	
	
	public class Client extends Sprite 
	{
		[Embed(source='./lib/25000_1_1_0_-16_-16.png')]
		public var mouseImg:Class;
		
		
		private function bug1( event:Event ):void
		{
		}
		
		public function Client():void 
		{
			// 必须监听1个东西，要不其他模块会接受不到消息，可能被释放掉了。
			LogicEventManager.Instance().addEventListener( "bug1" , bug1 );
			ModuleEventManager.Instance().addEventListener( "bug1" , bug1 );
			UIEventManager.Instance().addEventListener( "bug1" , bug1 );
			
			RoleConfig.Instance();
			
			GameStageManager.Instance();
			ActionManager.Instance();
			
			GameResManager.Instance().init();
			GameNetManager.Instance().init();
			RenderManager.Instance().init( this );
			SoundManager.Instance().init();
			
			
			
			setStage();
			
			GameLoading.Instance().init( new Loading() );

			

			RenderManager.instance.stage = stage;
			
			LogManager.logOut( "Client start" );
			
			RenderManager.instance.Debug.addChild( DebugManager.Instance() );
			
			ModuleManager.Instance().init();			
				
			
			// mouse
//			var mouseSpr:Sprite = new Sprite();
//			mouseSpr.addChild( new mouseImg() );
//			mouseSpr.mouseChildren = false;
//			mouseSpr.mouseEnabled = false;
//			mouseSpr.startDrag();
//			RenderManager.instance.Debug.addChild( mouseSpr );
			var cursorData:MouseCursorData = new MouseCursorData();
			var bitmapDatas:Vector.<BitmapData> = new Vector.<BitmapData>(1,true);
			var bitmap:Bitmap = new mouseImg();
			bitmapDatas[0] = bitmap.bitmapData;
			cursorData.data = bitmapDatas;   
			
			Mouse.registerCursor( "cur1" , cursorData );
			Mouse.cursor = "cur1";
			
			
		}
		
		
		private function setStage():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.displayState = StageDisplayState.NORMAL;
			stage.align = StageAlign.TOP_LEFT;
			
			
			//stage.displayState = "fullScreen";
			
			
			stage.addEventListener( MouseEvent.CLICK , onClick );
			stage.showDefaultContextMenu = false;
			stage.addEventListener( Event.RESIZE , resizeHandler );
			
			stage.addEventListener(KeyboardEvent.KEY_UP , onKeyUp );
			
			RenderManager.instance.stageWidth = stage.stageWidth;
			RenderManager.instance.stageHeight = stage.stageHeight;
		}
		
		
		private static function onClick( e:MouseEvent ):void
		{
			trace("click" + e.target + "," + e.target.name );
		}
		
		
		public function onKeyUp( e:KeyboardEvent ):void
		{
			var event:ModuleEvent = new ModuleEvent( ModuleEvent.KEYCODE );
			event.object = e;
			ModuleEventManager.instance.dispatchEvent( event );
		}
		
		
		private function resizeHandler( event:Event ):void
		{
			if ( stage.stageWidth <= 0 || stage.stageHeight <= 0 )
			{
				return;
			}
			
			var newEvent:ModuleEvent = new ModuleEvent( ModuleEvent.RESIZE );
			
			RenderManager.instance.stageWidth = stage.stageWidth;
			RenderManager.instance.stageHeight = stage.stageHeight;
			
			if ( stage.stageWidth < 800 )
			{
				stage.stageWidth = 800;
			}
			
			if ( stage.stageHeight < 600 )
			{
				stage.stageHeight = 600;
			}
			
			GameLoading.instance.resize();
			MessageBox.reSize();
			
			//trace( stage.stageWidth , stage.stageHeight );
			ModuleEventManager.instance.dispatchEvent( newEvent );
		}
		
	}
	
}